/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.ts;

import com.hp.hpl.jena.vocabulary.XSD;
import edu.yonsei.iwb.ts.index.TSIndexFields;
import edu.yonsei.iwb.ts.model.ObjectNode;
import edu.yonsei.iwb.ts.model.Triple;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.terms.TermsFacet;

/**
 *
 * @author dk
 */
public class TSSearchResultBuilder {

    public static ArrayList<String> buildResultResourceSet(SearchResponse response, String field) throws IOException {

        SearchHits hits = response.getHits();
        int totalnum = (int) hits.getTotalHits();
        ArrayList<String> resultSet = new ArrayList();
        for (int i = 0; i < hits.hits().length; i++) {
            SearchHit sh = hits.getHits()[i];
            Map<String, Object> source = sh.getSource();
            Object value = source.get(field);
            resultSet.add(value.toString());
        }
        return resultSet;
    }

    public static ArrayList<ObjectNode> buildResultObjectSet(SearchResponse response) throws IOException {

        SearchHits hits = response.getHits();
        int totalnum = (int) hits.getTotalHits();
        ArrayList<ObjectNode> resultSet = new ArrayList();
        for (int i = 0; i < hits.hits().length; i++) {
            SearchHit sh = hits.getHits()[i];
            Map<String, Object> source = sh.getSource();
            Object value = source.get(TSIndexFields.OBJECT);
            Object valueType = source.get(TSIndexFields.OBJECT_TYPE);
            if(valueType == null){
                ObjectNode on = new ObjectNode(value.toString(), XSD.xstring.getURI());
                resultSet.add(on);
            }else{
                ObjectNode on = new ObjectNode(value.toString(), valueType.toString());                
                resultSet.add(on);
            }
        }
        return resultSet;
    }

    public static ArrayList<Triple> buildResultOfAllvalue(SearchResponse response) throws IOException {
        SearchHits hits = response.getHits();
        ArrayList<Triple> resultSet = new ArrayList();
        for (int i = 0; i < hits.hits().length; i++) {
            SearchHit sh = hits.getHits()[i];
            Map<String, Object> source = sh.getSource();
            try {
                String subject = source.get(TSIndexFields.SUBJECT).toString();
                String predicate = source.get(TSIndexFields.PREDICATE).toString();
                String value = source.get(TSIndexFields.OBJECT).toString();
                Object valueType = source.get(TSIndexFields.OBJECT_TYPE);
                String vt;
                if(valueType == null){
                    vt = XSD.xstring.getURI();                    
                }else{
                    vt= valueType.toString();
                }

                Triple tp = new Triple(subject, predicate, value, vt);
                resultSet.add(tp);
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }
        }
        return resultSet;
    }

    public static String buildGroupByResult(SearchResponse response, String facetField) throws IOException {

        TermsFacet tfset = response.getFacets().facet(facetField);
        XContentBuilder doc = XContentFactory.jsonBuilder().prettyPrint().startObject().startArray("rows");
        LinkedHashMap fieldValue = new LinkedHashMap();
        ArrayList<String> fieldList = new ArrayList();
        for (int j = 0; j < tfset.getEntries().size(); j++) {
            String field = tfset.getEntries().get(j).getTerm().string();
            fieldList.add(field);
            fieldValue.put(field, tfset.getEntries().get(j).getCount());
//                doc.field(tfset.getEntries().get(j).getTerm().string(), tfset.getEntries().get(j).getCount());
        }
        doc.value(fieldValue);
        doc.endArray();
        doc.field("colNames", fieldList);
        doc.field("total", tfset.getEntries().size());
        doc.field("result_cd", "0000");
        doc.field("result_msg", "complete").endObject();
        return doc.string();
    }
}
